//
//  ESTCarModelTableViewController.m
//  ESTSCRMAPP
//
//  Created by 李贤立 on 2018/10/18.
//  Copyright © 2018年 Capfer. All rights reserved.
//

#import "ESTCarModelTableViewController.h"
#import "ESTCarBrandModel.h"
#import "ESTCarBrandSeriesConfigSectionHeaderView.h"

@interface ESTCarModelTableViewController ()
/**
 *  数据源
 */
@property(nonatomic, strong) NSArray<ESTCarBrandConfigModel *> *dataArray;
@end

@implementation ESTCarModelTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.mainTitle = [@"选择车型" S];
    self.tableView.tableFooterView = [[TYZView alloc] init];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self acquiredCarModel];
}
- (void)acquiredCarModel {
    WEAKSELF
    [TYZLoadingView showMessage:nil];
    [ESTCarBrandModel enquiriesCarConfigWithDLR_ID:self.DLR_ID brand_id:self.model.CAR_BRAND_ID series_id:self.model.CAR_SERIES_ID success:^(NSArray<ESTCarBrandConfigModel *> *modelArray) {
        [TYZLoadingView hideHUD];
        weakSelf.dataArray = modelArray;
        [weakSelf.tableView reloadData];
    } failure:^(NSString *error) {
        [TYZLoadingView showError:NetworkUnusual];
    }];
}
- (void)setDataArray:(NSArray<ESTCarBrandConfigModel *> *)dataArray {
    _dataArray = dataArray;
    if (dataArray.count > 0) {
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        [self.tableView removeNoDataImage];
        [self.tableView reloadData];
    } else {
        WEAKSELF
        [self.tableView noDataWithClick:^{
            [weakSelf acquiredCarModel];
        }];
    }
}
#pragma mark - Table view data source
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    ESTCarBrandSeriesConfigSectionHeaderView *headerView = [ESTCarBrandSeriesConfigSectionHeaderView creatCarBrandSeriesConfigSectionHeaderView:tableView];
    headerView.leftTitle = self.model.CAR_SERIES_CN;
    headerView.rightValue = self.dataArray.count;
    return headerView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ESTCarModelTableViewController"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ESTCarModelTableViewController"];
    }
    cell.textLabel.text = self.dataArray[indexPath.row].CAR_CONFIG_CN;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.selectedCarModelBlock) {
        self.selectedCarModelBlock(self.dataArray[indexPath.row].CAR_CONFIG_CN, self.dataArray[indexPath.row].CAR_CONFIG_ID);
        [self.navigationController popViewControllerAnimated:YES];
    }
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
